﻿using System.Collections.Generic;
using MvcCms.Core.Cache;
using MvcCms.Core.Interfaces;
using MvcCms.Data;


namespace MvcCms.Core
{
    public class MvcCmsValidation : IMvcCmsValidation
    {
        private readonly ICmsRepository _repository;
        private readonly ICacheService _cacheService;

        public MvcCmsValidation(ICmsRepository repository, ICacheService cacheService)
        {
            _repository = repository;
            _cacheService = cacheService;
        }

        #region IEnforceCmsPortalRules Members

        public bool ValidateCategory(ContentCategory categoryToValidate, ref string message)
        {

            var currentPortal = _cacheService.GetCurrentPortal();

            //find out if path already exists
            ContentCategory cat2Check = _repository.GetContentCategoryByFullPath(categoryToValidate.Path,
                                                                                 categoryToValidate.Portal);
            if (cat2Check != null && cat2Check.CategoryID != categoryToValidate.CategoryID)
            {
                message="The path from title '" + categoryToValidate.Path +
                                               "' is already being used, please adjust your title.";
                return false;
            }
            if (categoryToValidate.ParentCategory != null)
            {
                //find out if page path exists in this category
                IEnumerable<ContentPage> pages = _repository
                    .ListPagesByCat(categoryToValidate.ParentCategory.CategoryID, currentPortal);
                foreach (ContentPage page in pages)
                {
                    if (page.Path == categoryToValidate.SinglePath)
                    {
                        message="The path from title '" + categoryToValidate.Path +
                                                       "' already exists as a page for the parent category, please revise to save the page.";
                        return false;
                    }
                }
            }
            return true;
        }

        public bool ValidatePage(ContentPage pageToValidate, ref string message)
        {

            var currentPortal = _cacheService.GetCurrentPortal();
          
            if (!pageToValidate.IsCategoryPage && pageToValidate.ParentLanguageVersion ==null)
            {
                //find out if path exists in this category
                IEnumerable<ContentPage> pages = _repository
                    .ListPagesByCat(pageToValidate.ContentCategory.CategoryID, currentPortal);
                foreach (ContentPage page in pages)
                {
                    if (page.Path == pageToValidate.Path && page.ContentPageID != pageToValidate.ContentPageID)
                    {
                        message="The path created from the title already exists as a page, please revise to save the page.";
                        return false;
                    }
                }
                ContentCategory cat2Check = _repository.GetContentCategoryByPath(pageToValidate.Path,
                                                                                 pageToValidate.Portal);
                if (cat2Check != null)
                {
                    message="The path from title '" + pageToValidate.Path +
                                                   "' is already being used for a category, please revise to save the page.";
                    return false;
                }
            }

            return true;
        }

        #endregion
    }
}